<template>
  <view class="container">
    <form @submit="submitForm">
      <view class="form-group">
        <text class="label">时间</text>
        <picker style="border: 2px solid lightgrey;" mode="date" v-model="time" @change="handleTimeChange">
          <view class="picker">
            {{time}}
          </view>
        </picker>
      </view>
      <view class="form-group">
        <text class="label">体温</text>
        <input  style="border: 2px solid lightgrey;" type="number" name="temperature" placeholder="请输入体温" v-model="temperature" @input="handleInputChange" />
      </view>
      <view class="form-group">
        <text class="label">症状</text>
			<radio-group class="uni-list" @change="handleSymptomChange">
					<label class="uni-list-cell uni-list-cell-pd" v-for="(item,index) in symptoms" :key="index">
						<view>
							<radio :id="item.name" :value="item.value" :checked="item.checked"></radio>
							<label class="label-2-text" :for="item.name">
								<text>{{item.value}}</text>
							</label>
						</view>
					</label>
				</radio-group>
      </view>
      <view class="form-group">
        <text class="label">备注</text>
        <textarea name="remark" placeholder="请输入备注" v-model="remark" @input="handleInputChange"></textarea>
      </view>
      <button type="submit" @click="submitForm" class="submit-btn">提交</button>
    </form>
  </view>
</template>

<script>
	import dayjs from "dayjs"
export default {
  data() {
    return {
      time: dayjs(new Date()).format('YYYY-MM-DD'),
      temperature: '',
      symptoms: [
        { name: '正常', value: '正常', checked: false },
        { name: '感冒', value: '感冒', checked: false },
        { name: '发烧', value: '发烧', checked: false },
        { name: '四肢酸痛', value: '四肢酸痛', checked: false },
		{name:'其他',value:'其他',checked:false}
      ],
	  symptom:'',
      remark: '',
	  info:'',
    }
  },
  mounted(){
	  let that = this
	  uni.getStorage({
	  	key:'info',
	  	success:function(res){
	  		that.info = JSON.parse(res.data)}
	})
	
  },
  methods: {
    handleTimeChange(e) {
      this.time = e.detail.value
    },
    handleInputChange(e) {
      this[e.target.name] = e.target.value
    },
    handleSymptomChange(e) {
      const values = e.detail.value
      this.symptoms.forEach(item => {
        item.checked = values.indexOf(item.value) !== -1
      })
    },
    submitForm() {
		
		for(let i=0;i<this.symptoms.length;i++){
			if(this.symptoms[i].checked===true){
				this.symptom = this.symptoms[i].value
			}
		}
		uni.request({
			url:'http://localhost:8085/api/health/add',
			method:'POST',
			header : {'content-type':'application/x-www-form-urlencoded'},
			data:{
				user_id:this.info.user_id,
				temperature:this.temperature,
				name:this.info.name,
				symptom:this.symptom,
				dept_id:this.info.dept_id,
				date:dayjs(this.time).format('YYYY-MM-DD'),
				remark:this.remark
			},
			success:function(res){
				let response = res.data 
				if(response.code ===200){
					uni.showToast({
						title:'上报成功',
						
					})
				}else{
					uni.showToast({
						title:'失败',
						icon:'error'
					})
				}
				
			}
		})
	  console.log(this.time,this.temperature,this.remark,this.symptom,this.symptoms)
      // 这里是提交表单的代码
    }
  }
}
</script>
<style>
/* index.css */
.container {
  padding: 20rpx;
}

.form-group {
  margin-bottom: 20rpx;
}

.label {
  display: inline-block;
  width: 80rpx;
  text-align: right;
  margin-right: 20rpx;
}

.picker {
  display: inline-block;
  margin-left: 20rpx;
}

.unit {
  display: inline-block;
  margin-left: 10rpx;
}

.checkbox {
  display: flex;
  align-items: center;
  margin-bottom: 10rpx;
}

.checkbox-label {
  margin-left: 10rpx;
}

textarea {
  width: 100%;
  height: 80rpx;
  padding: 10rpx;
  font-size: 32rpx;
  border: 1rpx solid #ddd;
  border-radius: 5rpx;
  resize: none;
}

.submit-btn {
  display: block;
  width: 100%;
  height: 88rpx;
  line-height: 88rpx;
  text-align: center;
  background-color: #007AFF;
  color: #fff;
  font-size: 36rpx;
  border: none;
  border-radius: 5rpx;
  outline: none;
}

</style>